package com.example.work4;

import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    Button btnBindService,btnUnbindService,btnGetStatus;
    TextView tvServiceStatus;
    EditText numa, numb;
    MyService.MyServiceBinder serviceBinder;
    boolean isServiceBind=false;
    ServiceConnection conn=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
//返回 Service 的 Binder 对象
            System.out.println("连接");
            serviceBinder = (MyService.MyServiceBinder) service;
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        btnBindService=findViewById(R.id.btnBindService);
        btnUnbindService=findViewById(R.id.btnUnbindService);
        btnGetStatus=findViewById(R.id.btnGetStatus);
        tvServiceStatus=findViewById(R.id.tvServiceStatus);
        btnBindService.setOnClickListener(this);
        btnUnbindService.setOnClickListener(this);
        btnGetStatus.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent=new Intent();
        intent.setClass(MainActivity.this,MyService.class);
        switch (v.getId()){
            case R.id.btnBindService:
//如果 service 尚未绑定就绑定，如果已经绑定则忽略
                if(!isServiceBind){
                    startService(intent);
                    bindService(intent, conn, BIND_AUTO_CREATE);
                    isServiceBind = true;
                    btnGetStatus.setText("当前状态服务状态：连接成功");
                }
                break;
            case R.id.btnUnbindService:
//如果 service 已经绑定，获取 service 的 count 计数并显示在截面上。break;
                if(isServiceBind){
                    unbindService(conn);
                    isServiceBind = false;
                    stopService(intent);
                    btnGetStatus.setText("当前状态服务状态：断开连接");
                }
            case R.id.btnGetStatus:
//如果 service 已经绑定，则可以解绑，否则忽略
                if(isServiceBind){
                    if(!numa.getText().toString().equals("") && !numb.getText().toString().equals("")){
                        String res = serviceBinder.getResult(numa.getText().toString(), numb.getText().toString()) + "";
                        tvServiceStatus.setText("计算结果为：" + res);
                        btnGetStatus.setText("当前状态服务状态：计算成功");
                    }
                    else{
                        tvServiceStatus.setText("请输入要计算的数字！");
                    }
                }
                else{
                    btnGetStatus.setText("当前状态服务状态：未连接");
                }
                break;
        }
    }
}
